package com.qijian.tool.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.qijian.tool.domain.JSONCovert;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class ItemAttrUtils {

    public static String covert(String itemAttr) {

        JSONArray jsonArray = JSON.parseArray(itemAttr);
        // 直接转换JSONArray到List集合
        List<JSONCovert> list = jsonArray.toJavaList(JSONCovert.class);
        StringBuilder stringBuilder = new StringBuilder();
        for (JSONCovert jsonCovert : list) {
            stringBuilder.append(jsonCovert.getName()).append(":").append(jsonCovert.getValue()).append(",");
        }
        return stringBuilder.toString();
    }


    public static Map<String, String> covertMap(String itemAttr) {

        JSONArray jsonArray = JSON.parseArray(itemAttr);
        // 直接转换JSONArray到List集合
        List<JSONCovert> list = jsonArray.toJavaList(JSONCovert.class);

        return list.stream().collect(Collectors.toMap(JSONCovert::getName, JSONCovert::getValue, (a, b) -> b));
    }


}
